﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Net;
using System.IO;
using System.Data;

namespace EP2_Gameserver
{
    class Program
    {

        public static int userid;
        public static Byte[] bytes;
        public static NetworkStream stream;

        static void Main(string[] args)
        {

            try
            {

                Config.Read();
                Config.newCharacter();

                mySQL.Test();

                Data.GetNPCSpawnList();



                TcpListener server = new TcpListener(Config.GameServerIP, Config.GameServerPort);

                server.Start();

                bytes = new Byte[256];

                LogScreen.Info("EP2 GameServer listening on " + Config.GameServerIP.ToString() + ":" + Config.GameServerPort.ToString());


                // Enter the listening loop.
                while (true)
                {
                  
                    TcpClient client = server.AcceptTcpClient();
                    
                    stream = client.GetStream();
                    
                    int i;

                    while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
                    {
                        byte Opcode = bytes[17];

                        switch (Opcode)
                        {
                            case 0x03: 
                                Protocols.Received_Login(); 
                                break;

                            case 0x04:
                                switch (bytes[18])
                                {
                                    case 0x00:
                                        Protocols.Received_CharacterCreated();
                                        break;

                                    case 0x02:
                                        Protocols.Received_CharScreen_CharSelected();
                                        break;
                                }
                                break;

                            case 0x05: 
                                Protocols.Received_WorldLoaded(); 
                                break;

                            case 0x0C:
                                switch (bytes[19])
                                {
                                    case 0x01:
                                        Protocols.Received_CharacterMoving();
                                        break;

                                    case 0x03:
                                        Protocols.Received_CharacterStopMoving();
                                        break;
                                }
                                break;
                        }

                    }

                    client.Close();
                }
            }
            catch (SocketException e)
            {
                Console.WriteLine("SocketException: {0}", e);
            }

        }

    }
}
